<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="活跃状态" prop="activeState">
        <el-input v-model="formData.activeState" placeholder="请输入活跃状态" />
      </el-form-item>
      <el-form-item label="岗位名称" prop="jobName">
        <el-input v-model="formData.jobName" placeholder="请输入岗位名称" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入电话" />
      </el-form-item>
      <el-form-item label="工作时长" prop="jonLong">
        <el-input v-model="formData.jonLong" placeholder="请输入工作时长" />
      </el-form-item>
      <el-form-item label="工作意向" prop="jobState">
        <el-input v-model="formData.jobState" placeholder="请输入工作意向" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="formData.sex">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="formData.age" placeholder="请输入年龄" />
      </el-form-item>
      <el-form-item label="最高学历" prop="education">
        <el-input v-model="formData.education" placeholder="请输入最高学历" />
      </el-form-item>
      <el-form-item label="优势" prop="advantage">
        <el-input v-model="formData.advantage" placeholder="请输入优势" />
      </el-form-item>
      <el-form-item label="简历分数" prop="resumeSource">
        <el-input v-model="formData.resumeSource" placeholder="请输入简历分数" />
      </el-form-item>
      <el-form-item label="权限 默认私有" prop="permission">
        <el-input v-model="formData.permission" placeholder="请输入权限 默认私有" />
      </el-form-item>
      <el-form-item label="是否候选" prop="candidate">
        <el-radio-group v-model="formData.candidate">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否邀面" prop="invitation">
        <el-radio-group v-model="formData.invitation">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否储备" prop="reserve">
        <el-radio-group v-model="formData.reserve">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否合适" prop="admission">
        <el-radio-group v-model="formData.admission">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="工作状态" prop="workingStatus">
        <el-radio-group v-model="formData.workingStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { PersonnelResumeApi, PersonnelResumeVO } from '@/api/hrm/personnelresume'

/** 简历库 表单 */
defineOptions({ name: 'PersonnelResumeForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  activeState: undefined,
  jobName: undefined,
  phone: undefined,
  jonLong: undefined,
  jobState: undefined,
  sex: undefined,
  age: undefined,
  education: undefined,
  advantage: undefined,
  resumeSource: undefined,
  permission: undefined,
  candidate: undefined,
  invitation: undefined,
  reserve: undefined,
  admission: undefined,
  workingStatus: undefined,
})
const formRules = reactive({
  name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
  permission: [{ required: true, message: '权限 默认私有不能为空', trigger: 'blur' }],
  candidate: [{ required: true, message: '是否候选不能为空', trigger: 'blur' }],
  invitation: [{ required: true, message: '是否邀面不能为空', trigger: 'blur' }],
  reserve: [{ required: true, message: '是否储备不能为空', trigger: 'blur' }],
  admission: [{ required: true, message: '是否合适不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PersonnelResumeApi.getPersonnelResume(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PersonnelResumeVO
    if (formType.value === 'create') {
      await PersonnelResumeApi.createPersonnelResume(data)
      message.success(t('common.createSuccess'))
    } else {
      await PersonnelResumeApi.updatePersonnelResume(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    activeState: undefined,
    jobName: undefined,
    phone: undefined,
    jonLong: undefined,
    jobState: undefined,
    sex: undefined,
    age: undefined,
    education: undefined,
    advantage: undefined,
    resumeSource: undefined,
    permission: undefined,
    candidate: undefined,
    invitation: undefined,
    reserve: undefined,
    admission: undefined,
    workingStatus: undefined,
  }
  formRef.value?.resetFields()
}
</script>